1 // ----------------------------------------------------------------------------
2 // <copyright file=
"CustomTypes.cs" company="Exit Games GmbH">
3 // PhotonNetwork Framework
for Unity - Copyright (C) 2011 Exit Games GmbH
4 // </copyright>
5 // <summary>
6 //
7 // </summary>
8 // <author>developer@exitgames.com</author>
9 // ----------------------------------------------------------------------------

10
11 #pragma warning disable
1587
12 ///
\file
13 ///
<summary>Sets up support for Unity-specific types. Can be a blueprint how to register your own Custom Types for sending.</summary>
14 #pragma warning restore
1587
15
16
17 using
System.IO;
18 using
System.Runtime.InteropServices;
19 using
ExitGames.Client.Photon;
20 using
System;
21 using
UnityEngine;
22
23
24
25 ///
<summary>
26 ///
Internally used class, containing de/serialization methods for various Unity-specific classes.
27 ///
Adding those to the Photon serialization protocol allows you to send them in events, etc.
28 ///
</summary>
29 internal
static class CustomTypes
30 {

31     ///
<summary>Register</summary>
32     
internal static void Register()
33     {
34         PhotonPeer.RegisterType(
typeof(Vector2), (byte)'W', SerializeVector2, DeserializeVector2);
35         PhotonPeer.RegisterType(
typeof(Vector3), (byte)'V', SerializeVector3, DeserializeVector3);
36         PhotonPeer.RegisterType(
typeof(Quaternion), (byte)'Q', SerializeQuaternion, DeserializeQuaternion);
37         PhotonPeer.RegisterType(
typeof(PhotonPlayer), (byte)'P', SerializePhotonPlayer, DeserializePhotonPlayer);
38     }
39
40
41     
#region Custom De/Serializer Methods
42
43
44     
public static readonly byte[] memVector3 = new byte[3 * 4];
45     
private static short SerializeVector3(MemoryStream outStream, object customobject)
46     {
47         Vector3 vo = (Vector3)customobject;
48
49         
int index = 0;
50         
lock (memVector3)
51         {
52             
byte[] bytes = memVector3;
53             Protocol.Serialize(vo.x, bytes,
ref index);
54             Protocol.Serialize(vo.y, bytes,
ref index);
55             Protocol.Serialize(vo.z, bytes,
ref index);
56             outStream.Write(bytes,
0, 3 * 4);
57         }
58
59         
return 3 * 4;
60     }
61
62     
private static object DeserializeVector3(MemoryStream inStream, short length)
63     {
64         Vector3 vo =
new Vector3();
65         
lock (memVector3)
66         {
67             inStream.Read(memVector3,
0, 3 * 4);
68             
int index = 0;
69             Protocol.Deserialize(
out vo.x, memVector3, ref index);
70             Protocol.Deserialize(
out vo.y, memVector3, ref index);
71             Protocol.Deserialize(
out vo.z, memVector3, ref index);
72         }
73
74         
return vo;
75     }
76
77
78     
public static readonly byte[] memVector2 = new byte[2 * 4];
79     
private static short SerializeVector2(MemoryStream outStream, object customobject)
80     {
81         Vector2 vo = (Vector2)customobject;
82         
lock (memVector2)
83         {
84             
byte[] bytes = memVector2;
85             
int index = 0;
86             Protocol.Serialize(vo.x, bytes,
ref index);
87             Protocol.Serialize(vo.y, bytes,
ref index);
88             outStream.Write(bytes,
0, 2 * 4);
89         }
90
91         
return 2 * 4;
92     }
93
94     
private static object DeserializeVector2(MemoryStream inStream, short length)
95     {
96         Vector2 vo =
new Vector2();
97         
lock (memVector2)
98         {
99             inStream.Read(memVector2,
0, 2 * 4);
100             
int index = 0;
101             Protocol.Deserialize(
out vo.x, memVector2, ref index);
102             Protocol.Deserialize(
out vo.y, memVector2, ref index);
103         }
104
105         
return vo;
106     }
107
108
109     
public static readonly byte[] memQuarternion = new byte[4 * 4];
110     
private static short SerializeQuaternion(MemoryStream outStream, object customobject)
111     {
112         Quaternion o = (Quaternion)customobject;
113
114         
lock (memQuarternion)
115         {
116             
byte[] bytes = memQuarternion;
117             
int index = 0;
118             Protocol.Serialize(o.w, bytes,
ref index);
119             Protocol.Serialize(o.x, bytes,
ref index);
120             Protocol.Serialize(o.y, bytes,
ref index);
121             Protocol.Serialize(o.z, bytes,
ref index);
122             outStream.Write(bytes,
0, 4 * 4);
123         }
124
125         
return 4 * 4;
126     }
127
128     
private static object DeserializeQuaternion(MemoryStream inStream, short length)
129     {
130         Quaternion o =
new Quaternion();
131
132         
lock (memQuarternion)
133         {
134             inStream.Read(memQuarternion,
0, 4 * 4);
135             
int index = 0;
136             Protocol.Deserialize(
out o.w, memQuarternion, ref index);
137             Protocol.Deserialize(
out o.x, memQuarternion, ref index);
138             Protocol.Deserialize(
out o.y, memQuarternion, ref index);
139             Protocol.Deserialize(
out o.z, memQuarternion, ref index);
140         }
141
142         
return o;
143     }
144
145     
public static readonly byte[] memPlayer = new byte[4];
146     
private static short SerializePhotonPlayer(MemoryStream outStream, object customobject)
147     {
148         
int ID = ((PhotonPlayer)customobject).ID;
149
150         
lock (memPlayer)
151         {
152             
byte[] bytes = memPlayer;
153             
int off = 0;
154             Protocol.Serialize(ID, bytes,
ref off);
155             outStream.Write(bytes,
0, 4);
156             
return 4;
157         }
158     }
159
160     
private static object DeserializePhotonPlayer(MemoryStream inStream, short length)
161     {
162         
int ID;
163         
lock (memPlayer)
164         {
165             inStream.Read(memPlayer,
0, length);
166             
int off = 0;
167             Protocol.Deserialize(
out ID, memPlayer, ref off);
168         }
169
170         
if (PhotonNetwork.networkingPeer.mActors.ContainsKey(ID))
171         {
172             
return PhotonNetwork.networkingPeer.mActors[ID];
173         }
174         
else
175         {
176             
return null;
177         }
178     }
179
180     
#endregion
181 }


----------------------------------------------------------------------------

PhotonNetwork Framework for Unity - Copyright (C) 2011 Exit Games GmbH

developer@exitgames.com

----------------------------------------------------------------------------

\file

Sets up support for Unity-specific types. Can be a blueprint how to register your own Custom Types for sending.

Internally used class, containing deserialization methods for various Unity-specific classes.

Adding those to the Photon serialization protocol allows you to send them in events, etc.

Register




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.504 lượt xem

Gõ tìm kiếm nhanh...